package com.sk.ccflower.server.lib.sysUtils;

import java.io.Serializable;

import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;

import com.sun.messaging.ConnectionConfiguration;
import com.sun.messaging.ConnectionFactory;
import com.sun.messaging.Destination;
import com.sun.messaging.jms.Connection;
import com.sun.messaging.jms.Session;

/**
 * @author eunwoogi
 * @since 2009. 12. 4.
 * Open Message Queue 서버로 데이터를 전송하는 도움 클래스
 */
public class MessageQueueUtil {
	
	public static int sendMsg( String url, String qName, Serializable bean ) throws Exception {
		ConnectionFactory myFac = null;
		Connection myConnection = null;
		Session mySession = null;
		Destination myDest = null;
		MessageProducer myProducer = null;
		
		if ( null == url )												return -1;
		if ( null == qName )											return -2;
		
		try {
			myFac = new ConnectionFactory();
			myFac.setProperty( ConnectionConfiguration.imqAddressList, url );
			myFac.setProperty( ConnectionConfiguration.imqReconnectEnabled, "true" );
			
			myConnection = (Connection) myFac.createConnection();
			
			mySession = (Session) myConnection.createSession( false, Session.AUTO_ACKNOWLEDGE );
			
			myDest = new com.sun.messaging.Queue( qName );
			
			myProducer = mySession.createProducer( myDest );
			
			ObjectMessage outMsg = mySession.createObjectMessage();
			outMsg.setObject( bean );
			
			myProducer.send( outMsg );
			
			return 0;
		} catch ( Exception e ) {
			e.printStackTrace();
			throw new Exception( e );
		} finally {
			try {
				if  ( null != mySession )		mySession.close();
				if  ( null != myConnection )	myConnection.close();
			} catch ( Exception e ) {
				throw new Exception( e );
			}
		}
	}
	
}
